//
// Globals.h -- Defines typedefs and global variables.
//

#ifndef GLOBALS_H
#define GLOBALS_H

#include <errno.h>

#define NULL 0

#define ASSERT(b)    ((b) ? (void)0 : panic_assert(__FILE__, __LINE__, #b))
#define PANIC(msg)   panic(msg)

#define ERROR(val) \
  if (processManager->getProcess()) \
  { \
    processManager->getProcess()->setErrno(val); \
  } \
  else \
  { \
    panic("Error, but no process running!"); \
  } 

#if DEBUG > 0
#  define DEBUG_MSG(msg) \
  kerr.write("[DEBUG\t] ", lightBlue); \
  kerr << "(" << dec << processManager->getProcess()->getPid() << ") " << msg << endl;
#else
# define DEBUG_MSG(msg)
#endif


#if DEBUG > 1
#  define DEBUG_VERBOSE(msg) \
  kerr.write("[VERBOSE] ", lightGreen); \
  kerr << "(" << dec << processManager->getProcess()->getPid() << ") " << msg << endl;
#else
# define DEBUG_VERBOSE(msg)
#endif

#if DEBUG > 2
#  define DEBUG_SPAM(msg) \
  kerr.write("[SPAM\t] ", yellow); \
  kerr << "(" << dec << processManager->getProcess()->getPid() << ") " << msg << endl;
#else
# define DEBUG_SPAM(msg)
#endif

typedef long unsigned int  size_t;
typedef unsigned long long u64int;
typedef          long long s64int;
typedef unsigned int    u32int;
typedef          int    s32int;
typedef unsigned short  u16int;
typedef          short  s16int;
typedef unsigned char   u8int;
typedef          char   s8int;

typedef unsigned int    Address;

extern "C" void memcpy(u8int *dest, u8int *src, u32int len);
extern "C" int  strcmp(char *str1, char *str2);

#ifdef SERIAL_OUT
# define serial kerr
#else
# define monitor kerr
#endif

extern class MonitorDriver       monitor;
extern class SerialDriver        serial;
extern class DescriptorTables    descriptorTables;
extern class Multiboot           multiboot;
extern class GenericFaultHandler genericHandler;
extern class PageFaultHandler    pageFaultHandler;
extern class Kernel              kernel;
extern class MemoryManagement    memoryManager;
extern class Timer              *timer;
extern class ProcessManager     *processManager;
extern class Elf32Parser         kernelElfParser;
extern class SyscallDispatcher   syscallDispatcher;
extern class Filesystem         *filesystem;
extern class InitrdFilesystem   *initrd;
extern class IRQDispatcher      *irqDispatcher;
extern class DebugExceptionHandler debugHandler;
extern class ResourceManager     resourceManager;

extern volatile u32int currentKernelStack;

void *operator new(size_t size);
void *operator new(size_t size, u32int place);
void *operator new(size_t size, bool pageAlign, u32int *physAddr=NULL);
void *operator new[](size_t size);
void *operator new[](size_t size, bool pageAlign, u32int *physAddr=NULL);
void  operator delete(void *p);
void  operator delete[](void *p);

extern void panic(const char *message);
extern void panic_assert(const char *file, u32int line, const char *desc);
extern void debug_message(const char *prefix, const char *msg);

extern void criticalSection();
extern void endCriticalSection();

#endif // GLOBALS_H
